﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Interop;

using SuntabuAssistant.AssistAchieve;
using SuntabuAssistant.AssistAchieve.Action;
using SuntabuAssistant.AssistAchieve.Hook;
using SuntabuAssistant.Voice2Redmine.Recognise;
using Application = System.Windows.Application;
using MessageBox = System.Windows.Forms.MessageBox;

namespace SuntabuAssistant.Interface
{
    /// <summary>
    ///     MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private const int disturbInterval = 60;
        private static MainWindow mw;

        protected static string appPath = AppDomain.CurrentDomain.BaseDirectory;
        private NotifyIcon notifyIcon;
        protected int times;

        
        
        protected MainWindow()
        {
            InitializeComponent();
            SetTitle();
            Notify();
            ResizeMode = ResizeMode.CanResizeWithGrip;


            Assistant assistant = Assistant.Instance;
            assistant.StartWorking();

           
        } 
   


        public bool IsZhun { get; set; }

        public static MainWindow Instance
        {
            get { return mw ?? (mw = new MainWindow()); }
        }


        private void SetTitle()
        {
            DateTime date = DateTime.Now;
            string str = "今天是" + date.ToLongDateString();
            Title = str;
        }

        private void Notify()
        {
            notifyIcon = new NotifyIcon {BalloonTipText = "Running,DND..."};
            notifyIcon.ShowBalloonTip(2000);
            notifyIcon.Text = "Running,DND...";
            notifyIcon.Icon = new Icon(appPath + "/res/icon1.ico");
            notifyIcon.Visible = true;
            //打开菜单项
            var open = new MenuItem("Open");
            open.Click += Show;
            //退出菜单项
            var exit = new MenuItem("Exit");
            exit.Click += Close;
            var login = new MenuItem("Login");
            login.Click += LoginEvent;
            //关联托盘控件
            var childen = new[] {open, login, exit};
            notifyIcon.ContextMenu = new ContextMenu(childen);

            notifyIcon.MouseDoubleClick += (o, e) => { if (e.Button == MouseButtons.Left) Show(o, e); };
        }

        private void LoginEvent(object sender, EventArgs e)
        {
            Login logWin = Login.Instance;
            logWin.Show();
            Hide();
        }

        private void Show(object sender, EventArgs e)
        {
            Visibility = Visibility.Visible;
            ShowInTaskbar = true;
            Activate();
        }

        private void Hide(object sender, EventArgs e)
        {
            ShowInTaskbar = false;
            Visibility = Visibility.Hidden;
        }

        private void Close(object sender, EventArgs e)
        {
            if (Assistant.Instance.IsZhun)
            {
                Application.Current.Shutdown();
            }
            else
            {
                MessageBox.Show("Go away,you can not do this!", "Hey!", MessageBoxButtons.OK);
            }
        }

        private void Window_StateChanged(object sender, EventArgs e)
        {
            WindowState ws = WindowState;

            if (ws == WindowState.Minimized)
            {
                Hide();
            }
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            e.Cancel = true;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Startup.SetKey();
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            OpenFile.OpenFolderAndSelectFile(@"E:\书籍");
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
           var dataPipe= VoiceText.RecordText(5);
            if (dataPipe!=null)
            {
                this.textBox1.Text = dataPipe.Recognised;
            }
         
        }
    }
}